package com.wanhelibs;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.wanhe.base.model.entity.TableResultEntity;
import com.wanhe.base.net.ServerApi;
import com.wanhe.base.runner.Runner;
import com.wanhe.base.util.DeviceUtil;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.base.websocket.WebSocketTool;
import com.wanhe.base.websocket.callback.LoginCallBack;
import com.wanhe.base.websocket.model.response.BaseResponse;

import org.java_websocket.handshake.ServerHandshake;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.net.URISyntaxException;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

import static org.junit.Assert.*;

/**
 * Created by cdj on 2017/7/12.
 */
@RunWith(AndroidJUnit4.class)
public class DataTest {

    public static final String TAG = "DataTest";

    @Test
    public void entryMethor() throws URISyntaxException {
        /**
         * 测试socket连接
         * @author 陈德基
         * @time 2017/7/29 14:59
         */
        WebSocketTool.getInstance().initWebSockClient();
        WebSocketTool.getInstance().setInitCallBack(new WebSocketTool.WebSocketInitCallBack() {
            @Override
            public void onOpen(ServerHandshake serverHandshake) {
                Logger.t(TAG).i("连接服务端websocket成功");
            }
            @Override
            public void onClose(int code, String reason, boolean remote) {
                Logger.t(TAG).e("连接服务端websocket失败");
                assertTrue(false);
            }
        });
        WebSocketTool.getInstance().connect();
    }

    @Test
    public void testLogin() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();
        /**
         * 测试登入
         * @author 陈德基
         * @time 2017/7/29 15:19
         */
        String deviceIDForServer = DeviceUtil.getDeviceIDForServer(appContext);
        assertFalse(TextUtils.isEmpty(deviceIDForServer));
        final int tag =(int)System.currentTimeMillis();
        WebSocketTool.getInstance().padLogin(tag, "002", "002", deviceIDForServer, new LoginCallBack() {
            @Override
            public void onlogin(String body) {
                assertFalse(TextUtils.isEmpty(body));
                Gson gson = new Gson();
                BaseResponse baseResponse = gson.fromJson(body, BaseResponse.class);
                assertTrue(baseResponse.getCode() == 0);
                long deltTime = System.currentTimeMillis() - tag;
                Logger.t(TAG).i("登入成功，登入耗时%d毫秒", deltTime);

                /**
                 * 登入成功后，进行开台测试
                 * 1，首先获取餐桌列表
                 * @author 陈德基
                 * @time 2017/7/29 15:26
                 */
                Runner runner = new Runner();
                ServerApi api = new ServerApi();
                runner.execute(api.GetTables("1", "16"), new Consumer<TableResultEntity>() {
                    @Override
                    public void accept(@NonNull TableResultEntity tableResultEntity) throws Exception {
                        assertTrue(ObjectUtil.checkObject(tableResultEntity));
                        assertTrue(tableResultEntity.resultType == TableResultEntity.SUCCESS);
                        assertTrue(ObjectUtil.checkObjectList(tableResultEntity.Tables));

                        /**
                         * 2，获取一个餐桌，然后进行开台操作
                         * @author 陈德基
                         * @time 2017/7/29 15:45
                         */

                    }
                });
            }
        });
    }

}
